class UserStoriesController < ApplicationController
  def new
    @user_story = UserStory.new
    @url = "../product_backlogs/create"
    
    render :layout => nil
  end

  def edit
    if(params[:id] != nil)
      @user_story = UserStory.find_by_id(params[:id])
      @url = "../product_backlogs/update"
      render :layout => nil
    else
      @url = "../product_backlogs/create"
      @user_story = UserStory.new(params[:user_story])
    end
  end
  
  def update
    @user_story = UserStory.find_by_id(params[:user_story][:id])
    if(@user_story != nil)
      @user_story.update_attributes(:headline => params[:user_story][:headline], :priority => params[:user_story][:priority], :description => params[:user_story][:description])
      redirect_to :controller => "product_backlogs", :action => "show"
    end
  end

  def delete
    @ids = params[:items].split(",")
    @not_deleted = Array.new
    @ids.each do |id|
      @id = UserStory.find_by_id(id)
      if(@id != nil)
        @id.destroy
      else
        @not_deleted.append(id)
      end
    end
    
    if(!@not_deleted.empty?)
      render :json => @not_deleted
    else
      render :text => "0"
    end
  end
  
  def show
    @user_story = UserStory.all
    render :layout => nil
  end

  def create
    @user_story = UserStory.new(params[:user_story])
    flash.keep(:notice)
    if(@user_story.save)
      flash[:notice] = "success"
      redirect_to :controller => "product_backlogs", :action => "show"#, :notice => "success"
    else
      flash[:notice] = "failure"
      render "product_backlogs/show"
      #redirect_to :controller => "user_stories", :action => "edit"#, :notice => "failure"
    end 
  end
end
